package main

import (
	"fmt"
	"github.com/hyperledger/fabric/core/chaincode/shim"
	pb "github.com/hyperledger/fabric/protos/peer"
)

type fabricStudy1 struct {
}

func (f *fabricStudy1) Init(stub shim.ChaincodeStubInterface) pb.Response {
	fmt.Println(" <<  ========  success init it is view in docker  ==========  >> ")
	return shim.Success([]byte("success init"))
}
/*
peer chaincode install -n fb14_cc_shbc_1222 -v 1.0 -p gitee.com/robertrfeng/fabric_cc_study/xiandong
系统初始化方法， 在部署chaincode的过程中当执行命令
peer chaincode instantiate -o orderer.qklszzn.com:7050 -C mytestchannel -n fb14_cc_shbc_1222 -v 1.0 -c '{"Args":["init","a","100","b","200"]}' -P "OR ('Org1MSP.member','Org2MSP.member')"
的时候会调用该方法
peer chaincode invoke -o orderer.qklszzn.com:7050  --waitForEvent -C mytestchannel -n fb14_cc_shbc_1221  -c '{"Args":["set","get","key1222","real cc MSP1 2019-12-22 10:22:36"]}'
peer chaincode query -C mytestchannel -n fb14_cc_shbc_1221 -c '{"Args":["get","get","key1222","chaincode v 1.5 = query11111"]}'
*/
func (f *fabricStudy1) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
	function,args := stub.GetFunctionAndParameters()
	var a_parm = args[0]
	var b_parm = args[1]
	var c_parm = args[2]
	fmt.Println("  ========  curr method is big invoke   ========== ")
	fmt.Printf(" parm is  %s  %s  %s  %s  \n " , a_parm , b_parm , c_parm ,function)

	if function == "get" {
		return f.get(stub, args)
	}else if function == "set" {
		return f.set(stub, args)
	}else if function == "delete" {
		return f.delete(stub, args)
	}
	return shim.Error("Invalid invoke function name. Expecting \"invoke\" \"query\"")
}

func (t *fabricStudy1) get(stub shim.ChaincodeStubInterface, args []string) pb.Response {
	var a_parm = args[0]
	var b_parm = args[1]
	var c_parm = args[2]
	fmt.Println("  ========  curr method is get   ========== ")
	fmt.Printf(" parm is  %s  %s  %s   \n " , a_parm , b_parm , c_parm )

	keyvalue,err := stub.GetState(b_parm)
	if( err != nil  ){

		return shim.Error(" finad error! ")
	}
	return shim.Success( keyvalue )
}

// test delete method
func (t *fabricStudy1) delete(stub shim.ChaincodeStubInterface, args []string) pb.Response {
	var a_parm = args[0]
	var b_parm = args[1]
	var c_parm = args[2]
	fmt.Println("  ========  curr method is delete   ========== ")
	fmt.Printf(" parm is  %s  %s  %s   \n " , a_parm , b_parm , c_parm )

	err := stub.DelState(b_parm)
	if err != nil {
		return shim.Error(" 删除出现错误！！！！！")
	}
	return shim.Success([]byte(" 删除正确！！！！！  "))
}

// test set method
func (t *fabricStudy1) set(stub shim.ChaincodeStubInterface, args []string) pb.Response {
	var a_parm = args[0]
	var b_parm = args[1]
	var c_parm = args[2]
	fmt.Println("  ========  curr method is set   ========== ")
	fmt.Printf(" parm is  %s  %s  %s  \n " , a_parm , b_parm , c_parm )
	stub.PutState(b_parm,[]byte(c_parm))
	return shim.Success( []byte( "success invok " + c_parm  )  )
}

func main() {
	err := shim.Start(new(fabricStudy1))
	if err != nil {
		fmt.Printf("Error starting Simple chaincode: %s", err)
	}
}

